package com.group.common.utils;

import com.group.neps.entity.City;
import com.group.neps.entity.Province;

import java.io.*;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 加载城市数据，配合ProvinceLoader类进行加载
 *
 * @author 刘海翔
 * @version 1.0
 * @see ProvinceLoader
 */
public class CityLoader {

    /**
     * 从 JSON 文件中加载城市数据，并将它们添加到对应省份的 cities HashMap 中
     *
     * @param provinceMap 已经加载好的省份 HashMap
     * @param filePath    城市 JSON 文件路径
     * @throws IOException 如果文件读取失败
     */
    public static void loadCities(HashMap<String, Province> provinceMap, String filePath) throws IOException {
        StringBuilder jsonContent = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String line;
        while ((line = reader.readLine()) != null) {
            jsonContent.append(line);
        }
        reader.close();

        String jsonStr = jsonContent.toString();
        Pattern pattern = Pattern.compile("\\{([^\\}]+)}");
        Matcher matcher = pattern.matcher(jsonStr);

        while (matcher.find()) {
            String objStr = matcher.group(1);
            String code = extractValue(objStr, "code");
            String name = extractValue(objStr, "name");
            String provinceCode = extractValue(objStr, "provinceCode");

            if (code != null && name != null && provinceCode != null) {
                City city = new City(code, name, provinceCode);
                Province province = provinceMap.get(provinceCode);
                if (province != null) {
                    province.getCities().put(code, city);
                }
            }
        }
    }
    /**
     * 加载城市数据，返回以城市名为键，城市编码为值的Map
     *
     * @param filePath 城市 JSON 文件路径
     * @return 城市数据，键为城市名，值为城市编码
     * @throws IOException 读取文件时发生IO异常
     */
    public static HashMap<String, String> loadCitiesReverse(String filePath) throws IOException {
        StringBuilder jsonContent = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String line;
        while ((line = reader.readLine()) != null) {
            jsonContent.append(line);
        }
        reader.close();

        String jsonStr = jsonContent.toString();
        Pattern pattern = Pattern.compile("\\{([^\\}]+)}");
        Matcher matcher = pattern.matcher(jsonStr);

        HashMap<String, String> cityReverseMap = new HashMap<>();

        while (matcher.find()) {
            String objStr = matcher.group(1);
            String code = extractValue(objStr, "code");
            String name = extractValue(objStr, "name");

            if (code != null && name != null) {
                cityReverseMap.put(name, code);
            }
        }

        return cityReverseMap;
    }

    /**
     * 辅助方法：从 JSON 片段中提取指定字段的值
     *
     * @param objStr JSON 片段
     * @param key 要提取的字段名
     */
    private static String extractValue(String objStr, String key) {
        Pattern pattern = Pattern.compile("\"" + key + "\":\"([^\"]+)\"");
        Matcher matcher = pattern.matcher(objStr);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }
}

